package net.xyz.dropitem;

import java.util.List;
import java.util.Random;

import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class Listening  implements Listener {
	private static List<String> worlds;
	private static Random rd=new Random();
	@EventHandler
	public void onPlayerBreak(BlockBreakEvent evt){
		if(!(worlds.contains(evt.getBlock().getWorld().getName()))&&!evt.getPlayer().isOp()){
			evt.setCancelled(true);
			if(evt.getPlayer().getWorld().getName().equals(DropItem.getCg().getString("TreeWorld"))){
				if(DropUtil.getTreeTypeMaterial().contains(evt.getBlock().getType())){
					ItemStack iStack=DropUtil.getDropTree();
					if(iStack!=null){
					evt.getPlayer().getWorld().dropItem(evt.getBlock().getLocation(),iStack);
					}
					DropUtil.clearTreeMaterials();
					if(evt.getPlayer().getInventory().getItemInMainHand()!=null&&evt.getPlayer().getInventory().getItemInMainHand().getType().getMaxDurability()>0){
						if(evt.getPlayer().getInventory().getItemInMainHand().hasItemMeta()){
							ItemMeta itemMeta =evt.getPlayer().getInventory().getItemInMainHand().getItemMeta();
							if(itemMeta.isUnbreakable()){
								return;
							}
						}
						short m=evt.getPlayer().getInventory().getItemInMainHand().getType().getMaxDurability();
						short s=evt.getPlayer().getInventory().getItemInMainHand().getDurability();
						evt.getPlayer().getInventory().getItemInMainHand().setDurability((short)(s+(rd.nextInt(4)+1)));
						s=(short) (s+1);
						if(s>m){
							evt.getPlayer().getInventory().setItemInMainHand(new ItemStack(Material.AIR));
							}
						}
				}
			}
			if(evt.getPlayer().getWorld().getName().equals(DropItem.getCg().getString("MineWorld"))){
				if(DropUtil.getMineTypeMaterial().contains(evt.getBlock().getType())){
					ItemStack iStack=DropUtil.getDropMine();
					if(iStack!=null){
					evt.getPlayer().getWorld().dropItem(evt.getBlock().getLocation(),iStack);
					}
					DropUtil.clearMineMaterials();
					if(evt.getPlayer().getInventory().getItemInMainHand()!=null&&evt.getPlayer().getInventory().getItemInMainHand().getType().getMaxDurability()>0){
						if(evt.getPlayer().getInventory().getItemInMainHand().hasItemMeta()){
							ItemMeta itemMeta =evt.getPlayer().getInventory().getItemInMainHand().getItemMeta();
							if(itemMeta.isUnbreakable()){
								return;
							}
						}
						short m=evt.getPlayer().getInventory().getItemInMainHand().getType().getMaxDurability();
						short s=evt.getPlayer().getInventory().getItemInMainHand().getDurability();
						evt.getPlayer().getInventory().getItemInMainHand().setDurability((short)(s+(rd.nextInt(4)+1)));
						s=(short) (s+1);
						if(s>m){
							evt.getPlayer().getInventory().setItemInMainHand(new ItemStack(Material.AIR));
							}
						}
					}
				}
			}
	}
	@EventHandler
	public void onPlayerPlace(BlockPlaceEvent evt){
		if(!(worlds.contains(evt.getBlock().getWorld().getName()))&&!evt.getPlayer().isOp()){
			evt.setCancelled(true);
		}
	}
	public static void setWorld(List<String> list) {
		if(list!=null){
		worlds=list;
		}
		else{
			worlds=null;
		}
	}
	
	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent evt){
		evt.setJoinMessage(DropItem.getCg().getString("JoinMessage"));
	}
	
	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent evt){
		evt.setQuitMessage(DropItem.getCg().getString("QuitMessage"));
	}
	
	@EventHandler
	public void onPlayerClick(PlayerInteractEvent evt){
			if(evt.getClickedBlock()!=null&&evt.getClickedBlock().getType()==Material.TRAP_DOOR){
				if(!evt.getPlayer().isOp()){
					evt.setCancelled(true);
				}
			}
	}
	@EventHandler
	public void onPlayerCreateV(VehicleCreateEvent evt){
			if(!(worlds.contains(evt.getVehicle().getWorld().getName()))){
				evt.getVehicle().remove();
			}
	}
	@EventHandler
	public void onPlayerEmptybukkit(PlayerBucketEmptyEvent evt){
		if(!(worlds.contains(evt.getPlayer().getWorld().getName()))&&!evt.getPlayer().isOp()){
			if(!evt.getPlayer().isOp()){
				evt.setCancelled(true);
			}
		}
}
}
